using Avalonia.BiliBili.ViewModels;
using Avalonia.Controls;
using Bilibili.Common;
using Bilibili.Common.Log;
using Bilibili.Model.Common;
using Bilibili.Model.Follow;
using Bilibili.WebClient.Api;
using PCLUntils.Objects;
using System;
using System.Threading.Tasks;

namespace Avalonia.BiliBili.Windows
{
    public partial class BangmiWindow : WindowBase
    {
        public BangmiClient Client { get; }
        private BangmiViewModel ViewModel { get; }
        public BangmiWindow()
        {
            InitializeComponent();
            Client = new BangmiClient();
            ViewModel = new BangmiViewModel();
        }
        protected override void AfterInitializeComponent()
        {
            this.DataContext = ViewModel;
            RunOnUiThread(async () => await LoadData(1, 1));
        }
        public void OnTabSelectionChanged(object? sender, SelectionChangedEventArgs e)
        {
            RunOnUiThread(async () =>
            {
                if (sender is TabControl control)
                    await LoadData(1, control.SelectedIndex + 1);
            });
        }
        private async Task LoadData(int page, int status)
        {
            try
            {
                if (ViewModel != null)
                {
                    var info = ViewModel.ListArray.First(x => x.Item1 == status);
                    if (!info.Item2 || page != info.Item3)
                    {
                        var result = await Client.GetFollow(SeasonType.bangumi, page, status);
                        if (result.IsSuccess())
                        {
                            var obj = result.Data;
                            if (obj.IsNotEmpty())
                            {
                                if (obj["code"].ToInt32() == 0)
                                {
                                    var data = obj["result"].ToObject<FollowResult>();
                                    if (data.follow_list != null)
                                    {
                                        foreach (var item in data.follow_list)
                                        {
                                            item._Status = status;
                                            switch (status)
                                            {
                                                case 1:
                                                    ViewModel.Watching.Add(item);
                                                    break;
                                                case 2:
                                                    ViewModel.Planing.Add(item);
                                                    break;
                                                case 3:
                                                    ViewModel.Watch.Add(item);
                                                    break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        ViewModel.ListArray.UpdateData(info.Item1, true, page);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("LoadData", ex);
            }
        }
    }
}